function [ W ] = MyDeconv2DFast( H,S,I,J, iter, saveing)
%MYDECONV2D Summary of this function goes here
%   Detailed explanation goes here
    
    
    [K,L] = size(S);
    SSum = zeros((I+K-1),(J+L-1));
    W = ones(I,J);
    W1 = W;
    for cou = 1:iter
        
        for m = 1:(I+K-1)
            for n = 1:(J+L-1)
                a = max(1,m-K+1);
                b = min(m,I);
                c = max(1,n-L+1);
                d = min(n,J);
                
                x2 = 0;
                for p = a:b
                    for q = c:d
                        x2 = x2+(W(p,q)*S(m-p+1,n-q+1));
                    end
                end
                SSum(m,n) = x2;
            end
        end
        
        for i = 1:I
            for j = 1:J
                y = 0;
                e = i+K-1;
                f = j+L-1;
                
                for m = i:e
                    for n = j:f
                        x1 = H(m,n)*S(m-i+1,n-j+1);
                        y = y+ (x1/SSum(m,n));
                    end
                end
                W1(i,j) = W(i,j)*y;
            end
        end
        
%         W = NewFullSize(W1, ceil(K/2),ceil(L/2));
        W = W1;
        
        if(saveing == 1)
            save(sprintf('tmp/W%d.mat', cou),  'W');
        end
    end
end

function y = NewFullSize(H, m, n)

%     y = H;
%     return;

    [mh,nh] = size(H);
    maxY = max(H(:));
    
    for i = m:(mh-m)
        H(i, 1:n) = H(i,n);
        H(i, (nh-n):nh) = H(i, (nh-n));
        H(i, 1:n) = maxY;
        H(i, (nh-n):nh) = maxY;
    end
    
    for j = 1:nh
        H(1:m, j) = H(m,j);
        H((mh-m):mh, j) = H((mh-m),j);
        H(1:m, j) = maxY;
        H((mh-m):mh, j) = maxY;
    end
    
    y = H;
end


